<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Model_Jurusan extends MY_Model {
	
	public function __construct() {

		parent::__construct();

		$this->table_name = 'aka_jurusan';

		$this->primary_key = 'aka_jurusan.jurusan_id';

	}
	function get_all($departemen_id,$aktif=null){
		$this->load->database();
			$filter = "";
			if($aktif != null)
				$filter = "AND {$this->table_name}.aktif='$aktif'";
			$sql = "SELECT {$this->table_name}.*,aka_departemen.nama as departemen FROM {$this->table_name} JOIN aka_departemen ON {$this->table_name}.departemen_id=aka_departemen.departemen_id WHERE {$this->table_name}.departemen_id = '$departemen_id' $filter ORDER BY {$this->primary_key} ASC";
			$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function get_by($id){
		$this->load->database();		
		$sql = "SELECT * FROM {$this->table_name} WHERE {$this->primary_key}='$id';";
		$query = $this->db->query($sql);
		$this->db->close();
		return $query;
	}
	
	function add($data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->insert($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function edit($id,$data){
		$this->load->database();
			$this->db->trans_start();
				$this->db->where($this->primary_key,$id);
				$this->db->update($this->table_name,$data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal disimpan.');
			}else{
				$this->session->set_flashdata('success','Data berhasil disimpan.');
			}
		$this->db->close();
	}
	
	function delete($id){
		$this->load->database();
			$this->db->trans_start();
				$sql = "DELETE FROM {$this->table_name} WHERE {$this->primary_key}='$id'";
				$this->db->query($sql);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
			{
				$this->session->set_flashdata('error','Data gagal dihapus.');
			}else{
				$this->session->set_flashdata('success','Data berhasil dihapus.');
			}
		$this->db->close();
	}
	
}

?>